/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 *
 *
 *@author joewee
 *@version 1.0.0
 *@Date 2018/2/25 22:08
 */
@Entity
@Table(name = "ADMIN_USER")
@Getter
@Setter
@Builder
@AllArgsConstructor
public class User extends BaseEntity implements Serializable{
    @Id
    @Column(name = "USER_ID", unique=true, nullable=false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name="ADMIN_NAME", length=100, unique=true)
    private String adminName;

    @ManyToMany(cascade = {CascadeType.REFRESH})
    @JoinTable(
            name = "USER_ROLE",
            joinColumns = @JoinColumn(name = "USER_ID", nullable = false),
            inverseJoinColumns = @JoinColumn(name = "ROLE_ID", nullable = false))
    private Set<Role> roles = new HashSet<>();

    @Column(name="ADMIN_PHONE")
    private String adminPhone;

    @Column(name="ADMIN_PASSWORD", length=60)
    @JsonIgnore
    private String adminPassword;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="MERCHANT_ID",foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    @JsonIgnore
    private MerchantStore merchantStore;

    @Column(name="ADMIN_LAST_NAME")
    private String lastName;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name="ACTIVE")
    private boolean active = true;

    @Column(name="ADMIN_Q1")
    private String question1;

    @Column(name="ADMIN_Q2")
    private String question2;

    @Column(name="ADMIN_Q3")
    private String question3;

    @Column(name="ADMIN_A1")
    private String answer1;

    @Column(name="ADMIN_A2")
    private String answer2;

    @Column(name="ADMIN_A3")
    private String answer3;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_ACCESS")
    private Date lastAccess;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LOGIN_ACCESS")
    private Date loginTime;

    //add by wqhe
    @Column(name="CONFIRM_CODE", length=6, unique=true, nullable=false)
    private String confirmCode;


    //add by wqhe
    @JsonIgnore
    @ManyToMany(fetch=FetchType.EAGER,cascade = {CascadeType.REFRESH})
    @JoinTable(
            name = "ADMIN_USER_MERCHANT",
            joinColumns = @JoinColumn(name = "USER_ID", nullable = false),
            inverseJoinColumns = @JoinColumn(name = "MERCHANT_ID", nullable = false))
    private Set<MerchantStore> merchantStores = new HashSet<>();


    public User() {

    }

    public User(String userName, String password, String phone) {

        this.adminName = userName;
        this.adminPassword = password;
        this.adminPhone = phone;
    }
}
